package day05.StringBuiders;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/26
 * /**
 *  * 1.使用System类获取当前系统的毫秒值
 *  * 统计代码的执行时间，String拼接+  、StringBuilder拼接
 *  */

public class Test1 {
    public static void main(String[] args) {
        int num = 10000;

        // 使用 + 运算符拼接字符串
        long startTime1 = System.currentTimeMillis();
        String result1 = "";
        for (int i = 0; i < num; i++) {
            result1 = result1 + "a";
        }
        long endTime1 = System.currentTimeMillis();
        long elapsedTime1 = endTime1 - startTime1;
        System.out.println("使用 + 运算符拼接字符串耗时: " + elapsedTime1 + " 毫秒");

        // 使用 StringBuilder 拼接字符串
        long startTime2 = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            sb.append("a");
        }
        long endTime2 = System.currentTimeMillis();
        long elapsedTime2 = endTime2 - startTime2;
        System.out.println("使用 StringBuilder 拼接字符串耗时: " + elapsedTime2 + " 毫秒");
    }
}
